import http from '../../http/http'
import * as url from "../../http/api";
import store from '..';
const state = {
    chooseItem: [],//选中的菜的数组
    demo: [
        // { name: "土豆烧排骨", classifyId: 1, price: 38, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "小鸡炖蘑菇", classifyId: 3, price: 38, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "水煮鱼", classifyId: 2, price: 38, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "湘西小炒肉", classifyId: 3, price: 38, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "干煸四季豆", classifyId: 3, price: 38, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "红烧磨合", classifyId: 1, price: 777, num: 0, file: require('../../assets/potato.jpg') },
        // { name: "钟林刺身", classifyId: 2, price: 888, num: 0, file: require('../../assets/potato.jpg') },
    ], //模拟后端菜谱
    showDemo: [],//模拟需要渲染的菜谱
    nowPage: 1,//菜谱当前页码
    seats: [
        // { id: 1, number: 1, peopleNum: 4, typeId: 0, type: "大厅", state: "可做", show: false, fullPrice: 0 },
        // { id: 2, number: 2, peopleNum: 5, typeId: 0, type: "大厅", state: "可做", show: false, fullPrice: 0 },
        // { id: 3, number: 3, peopleNum: 6, typeId: 1, type: "包间", state: "可做", show: false, fullPrice: 0 },
        // { id: 4, number: 4, peopleNum: 7, typeId: 1, type: "包间", state: "可做", show: false, fullPrice: 0 },
    ],//模拟餐桌
    showSeat: [],//需要渲染的餐桌
    tablePage: 1,
    // seatPageNum:1,//餐桌当前页面
    nowSeat: null, //点击入座的餐桌所在的数组的下标
    nowTablePrice: 0, //当前桌消费额
    nowPaySeat: null, //绑定的该桌所在的数组对应的下标
    seatsInfo: [], //记录每个餐桌下单之后的总金额和选的菜品
    needPayNum: 0,
}

const actions = {
    addSeat({ commit }, obj) { //添加餐桌
        let sendObj = {
            number: obj.number,
            peopleNum: obj.peopleNum,
            state: obj.state,
            type: obj.type,
            typeId: obj.typeId
        }
        return http({
            url: url.wzADDSEATS,
            method: 'post',
            data: sendObj
        }).then(res => {
            return http({
                url: url.wzSeats,
                method: "post",
                data: {}
            }).then(res => {
                commit('dealAddSeat', res.data)
            })
        }).catch(err => {
            console.log(err);
        })
        //通过http发送obj给后端，后端先加再查 然后给我数据
    },
    tableState({ commit }) { //确认下单后  发送改变后的餐桌状态
        let obj = {
            id: state.nowSeat + 1,
            state: '正在使用',
            time: store.getters.getDate
        }
        console.log(obj);
        // return http({
        //     url: url.wzSENDSEATSSTATE,
        //     method: 'post',
        //     data: obj
        // }).then(res => {
        //     console.log(res);
        // }).catch(err => {
        //     console.log(err);
        // })
    },
    addSeatinfo({ commit }, obj) { //下单时 记录桌号、消费金额、所选菜品
        // console.log(obj);
        let flag = state.seatsInfo.findIndex(item => item.id == obj.id)
        if (flag == -1) {
            state.seatsInfo.push(obj)
            commit('dealSeatInfo')
        } else {
            state.seatsInfo[flag].price += obj.price
            // console.log(...obj.item);
            state.seatsInfo[flag].item.push(...obj.item)
            // console.log(state.seatsInfo[flag].item);
            commit('dealSeatInfo')
        }
    },
    sendPayData({ commit }, obj) {//餐桌结账把 账单信息 发送给服务器记录
        // console.log(obj);
        // return http({
        //     url: url.wzPAY,
        //     method: 'post',
        //     data: obj
        // }).then(res => {
        //     commit('dealSendPayData',res)
        // }).catch(err => {
        //     console.log(err);
        // })
        console.log(obj);
        commit('dealSendPayData')
    },
    xcSearch({ commit }, obj) { //查询菜品
        // return http({
        //     url: url.wzSELECTDISH,
        //     method: 'post',
        //     data: obj
        // }).then(res => {
        //     commit('dealxcSearch', res.data)
        // }).catch(err => {
        //     console.log(err);
        // })
        commit('dealxcSearch', obj)
    },
    tableSearch({ commit }, obj) { //餐桌查询
        // return http({
        //     url: url.wzSELECTSEATS,
        //     method: 'post',
        //     data: obj
        // }).then(res => {
        //     commit('dealtableSearch', res.data)
        // }).catch(err => {
        //     console.log(err);
        // })
        commit('dealtableSearch', obj)
    },
}

const mutations = {
    dealAddSeat(context, obj) { //添加餐桌
        state.seats = []
        state.seats.push(...obj)
    },
    dealSeatInfo(context, obj) { //下单完成  菜品选中num归0
        state.demo.forEach(item => {
            item.num = 0
        })
        state.chooseItem = []
    },
    dealSendPayData(context, obj) {
        state.seatsInfo = state.seatsInfo.filter(item => {
            return item.id != state.nowPaySeat
        })
        // console.log(state.seatsInfo);
    },
    dealxcSearch(context, obj) {
        // console.log(obj);
        // state.showDemo = obj
        let { selectValue, inputValue } = obj
        state.showDemo = (state.demo.filter(item => item[selectValue] == inputValue))
    },
    dealtableSearch(context, obj) {
        // console.log(obj);
        // state.showSeat = obj
        let { selectValue, inputValue } = obj
        state.showSeat = (state.seats.filter(item => item[selectValue] == inputValue))
    }
}

const getters = {
    getDate() {
        let date = new Date();
        let seperator1 = "-";
        let seperator2 = ":";
        let month = date.getMonth() + 1;
        let strDate = date.getDate();
        let Hours = date.getHours();
        let Minutes = date.getMinutes();
        let Seconds = date.getSeconds();
        if (month >= 1 && month <= 9) {
            month = "0" + month;
        }
        if (strDate >= 0 && strDate <= 9) {
            strDate = "0" + strDate;
        }
        if (Hours >= 0 && Hours <= 9) {
            Hours = "0" + Hours;
        }
        if (Minutes >= 0 && Minutes <= 9) {
            Minutes = "0" + Minutes;
        }
        if (Seconds >= 0 && Seconds <= 9) {
            Seconds = "0" + Seconds;
        }
        let currentdate =
            date.getFullYear() +
            seperator1 +
            month +
            seperator1 +
            strDate +
            " " +
            Hours +
            seperator2 +
            Minutes +
            seperator2 +
            Seconds;
        return currentdate;
    }
}

export default {
    state,
    actions,
    mutations,
    getters
}











